﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Reflection;
using System.Threading;
using System.Globalization;
namespace TinyERP.Framework
{
    /// <summary>
    /// Class to manage resource from the main Application
    /// </summary>
    public class AppResourceManager
    {
        private const string APP_RESOURCE_FILE = "TinyERP.Framework.Resources.AppResource";
        private static AppResourceManager appResourceManager;
        private ResourceManager resources;
        private CultureInfo oldCultureInfo;
        private AppResourceManager()
        {
            try
            {
                resources = new ResourceManager(APP_RESOURCE_FILE, this.GetType().Assembly);
            }
            catch
            {
            }
        }
        public static AppResourceManager GetInstance()
        {
            if (appResourceManager == null)
            {
                appResourceManager = new AppResourceManager();
            }
            return appResourceManager;
        }
        public string GetString(string key)
        {
            string result = "";
            CultureInfo newCultureInfo = Thread.CurrentThread.CurrentUICulture;
            if (oldCultureInfo==null || oldCultureInfo.DisplayName != newCultureInfo.DisplayName)
            {
                oldCultureInfo = newCultureInfo;
                resources = new ResourceManager(APP_RESOURCE_FILE, this.GetType().Assembly);
            }

            try
            {
                result = resources.GetString(key);
            }
            catch
            {
                return key;
            }
            return result;
        }
    }
}
